//导入 基础工具
import {getURLBase64,getURLBase64Image,loadImages,drawImagesArea} from "./makeImgs-utils/getUrlImg"
//导入 无畏契约 生成方法
import {makeTitleImgWWQY,makeDetailImgWWQY} from "./makeImgs-utils/WuWeiQiYue"
//导入 和平精英 生成方法
import {makeTitleImgHpjy,makeDetailImgHPJY} from "./makeImgs-utils/HePingJingYing"
//导入 CF 穿越火线 生成方法
import {makeTitleImgCF,makeDetailImgCF} from "./makeImgs-utils/CF"
//导入 王者荣耀 生成方法
import {makeTitleImgWzry,makeDetailImgWzry} from "./makeImgs-utils/WangZheRongYao"
//导入 英雄联盟 LOL 生成方法
import {makeTitleImgLOL,makeDetailImgLOL} from "./makeImgs-utils/LOL"
//导入 英雄联盟手游 LOLm 生成方法
import {makeTitleImgLOLm,makeDetailImgLOLm} from "./makeImgs-utils/LOLm"
//导入 原神 生成方法
import {makeTitleImgYs} from "./makeImgs-utils/yuanshen"
//导入 永劫无间 生成方法
import {makeTitleImgYjwj,makeDetailImgYJWJ} from "./makeImgs-utils/YJWJ"
// 火影忍者  生成方法
import { makeTitleImgHYRZ ,makeDetailImgHYRZ} from "./makeImgs-utils/HuoyingRenzhe"
// 三角洲  生成方法
import { makeTitleImgSJZXD ,makeDetailImgSJZXD} from "./makeImgs-utils/SanJiaoZhou"

// 枪战王者  生成方法
import { makeTitleImgQZWZ ,makeDetailImgQZWZ} from "./makeImgs-utils/QiangzhanWangzhe"
// QQ飞车
import { makeTitleImgQQspeed } from "./makeImgs-utils/QQspeed"

// 生成英雄联盟战绩图
async function makeLoLzhanjiImg(data, str) {
  //数据
  ////console.log(data, str);
  let onedata = data.filter(item => item.battle_type == '0')
  ////console.log(onedata);
  let one = onedata[0]
  let twodata = data.filter(item => item.battle_type == '6')
  ////console.log(twodata);
  let two = twodata[0]
  let threedata = data.filter(item => item.battle_type == '7')
  ////console.log(threedata);
  let three = threedata[0]
  let fourdata = data.filter(item => item.battle_type == '8')
  ////console.log(fourdata);
  let four = fourdata[0]
  return new Promise((resolve, reject) => {
    getURLBase64(" https://imgage.maimaihao.com/makeImg/LOLZJ.png")
      .then((res) => {
        ////console.log(res);
        const imageaaa = new Image();
        imageaaa.src = res;
        imageaaa.onload = async () => {
          const canvas = document.createElement("canvas");
          const context = canvas.getContext("2d");
          canvas.width = imageaaa.width;
          canvas.height = imageaaa.height;
          context.drawImage(imageaaa, 0, 0, imageaaa.width, imageaaa.height);
          context.fillStyle = "#333333";
          context.font = "55px Adobe Ming Std";
          // ${one.total || "--"}
          context.fillText(`总局数`, 80, 215); //总局数
          context.fillText(`匹配赛`, 80, 379); //总局数
          context.fillText(`大乱斗`, 80, 539); //总局数
          context.fillText(`人机赛`, 80, 699); //总局数

          context.fillStyle = "#666666";
          context.font = "35px Adobe Ming Std";

          context.fillText(`${one.total ? one.total + '场' : '--'}`, 80, 262); //总局数
          context.fillText(`${four.total ? four.total + '场' : '--'}`, 80, 426); //总局数
          context.fillText(`${three.total ? three.total + '场' : '--'}`, 80, 586); //总局数
          context.fillText(`${two.total ? two.total + '场' : '--'}`, 80, 746); //总局数

          context.fillStyle = "#ffffff";
          context.font = "45px Adobe Ming Std";

          context.fillText(`${one.wins_rate ? '胜' + one.wins_rate + '%' : '--'}${one.wins ? '   ' + one.wins + '场' : '--'}`, 350, 235); //总局数
          context.fillText(`${four.wins_rate ? '胜' + four.wins_rate + '%' : '--'}${four.wins ? '   ' + four.wins + '场' : '--'}`, 350, 395); //总局数
          context.fillText(`${three.wins_rate ? '胜' + three.wins_rate + '%' : '--'}${three.wins ? '   ' + three.wins + '场' : '--'}`, 350, 556); //总局数
          context.fillText(`${two.wins_rate ? '胜' + two.wins_rate + '%' : '--'}${two.wins ? '   ' + two.wins + '场' : '--'}`, 350, 712); //总局数


          //  加载皮肤图片
          context.fillStyle = "#ffffff";
          context.font = "30px Adobe Ming Std";

          let img = await getURLBase64Image(str.rank_url);
          context.drawImage(img, 126, 900, img.width, img.height);


          context.fillStyle = "#000000";
          context.font = "45px Adobe Ming Std";

          context.fillText(`${str.full_rank_title ? str.full_rank_title : ''}`, 366, 1000);

          context.fillStyle = "#666666";
          context.font = "30px Adobe Ming Std";

          context.fillText(`${str.season_title ? str.season_title : ''}`, 88, 1077); //总局数
          //总局数

          context.fillStyle = "#000000";
          context.font = "40px Adobe Ming Std";
          context.fillText(`总场次`, 366, 1077); //总局数
          context.fillText(`胜点`, 678, 1077);
          context.fillText(`胜率`, 966, 1077);

          context.fillStyle = "#B7955A";
          context.font = "40px Adobe Ming Std";

          context.fillText(`${str.total ? str.total + '场' : ''}`, 500, 1077); //总局数
          context.fillText(`${str.wins ? str.wins + '场' : ''}`, 778, 1077);
          context.fillText(`${str.wins_rate ? str.wins_rate + '%' : ''}`, 1066, 1077);
          // 绘制结束
          const base64Url = canvas.toDataURL("image/jpeg", 0.8);
          resolve(base64Url);
        };
      })
      .catch((err) => {
        reject(err);
      });
  });
}
// 生成详情图-分多张长图
async function makeDetailImgMultiple(data, skins, attr) {
  return new Promise(async (resolve, reject) => {
    // 王者荣耀
    if(data.gameId == 2){
      resolve(makeDetailImgWzry(data, skins, attr))
      
    }
    // 英雄联盟
    if (data.gameId == 5) {
      resolve(makeDetailImgLOL(data, skins, attr))
    }
    // 英雄联盟手游
    if (data.gameId == 25) {
      resolve(makeDetailImgLOLm(data, skins, attr))
    }
    // 无畏契约
    if (data.gameId == 29) {
      resolve(makeDetailImgWWQY(data, skins, attr))
    }
    // 和平精英
    if(data.gameId == 3){
      resolve(makeDetailImgHPJY(data, skins, attr))
      
    }
    // CF 穿越火线
    if(data.gameId == 6){
      resolve(makeDetailImgCF(data, skins, attr))
      
    }
    //火影忍者
    if(data.gameId == 10){
      resolve(makeDetailImgHYRZ(data, skins, attr))
      
    }
     //永劫无间
     if(data.gameId == 12){
      resolve(makeDetailImgYJWJ(data, skins, attr))
      
    }
     //三三角洲
    if(data.gameId == 67){
      resolve(makeDetailImgSJZXD(data, skins, attr))
      
    }
     //枪战王者
    if(data.gameId == 8){
      resolve(makeDetailImgQZWZ(data, skins, attr))
      
    }
  })
}

// 生成详情图 通用
async function makeDetailImg(skins) {
  return new Promise(async (resolve, reject) => {
    let instancesUrl = skins;
    ////console.log("皮肤列表", skins);
    const length = instancesUrl.length;
    let instances = [];
    instances = await loadImages(instancesUrl, length);
    resolve(drawImages(instances));
  });
}
// 详情图写入图片  通用
function drawImages(images) {
  return new Promise((resolve, reject) => {
    const canvas = document.createElement("canvas");
    const context = canvas.getContext("2d");
    // 30个皮肤为一组
    let num = parseInt(images.length / 30) + 1; //一共有多少组
    if (images.length % 30 == 0) {
      num = num - 1;
    }
    canvas.width = 3000;
    canvas.height = 2100 * num;
    context.fillStyle = "#fff"; // 文字填充颜色
    context.font = "30px Adobe Ming Std";
    // 获取背景图
    // let bgImg https://imgage.maimaihao.com/logo/back-img.jpg
    getURLBase64(" https://imgage.maimaihao.com/makeImg/detail-bg.png")
      .then((res) => {
        const image = new Image();
        image.src = res;
        image.onload = () => {
          // 加载边框
          getURLBase64(" https://imgage.maimaihao.com/makeImg/skin-border.png")
            .then((border) => {
              const borderImg = new Image();
              borderImg.src = border;
              borderImg.onload = () => {
                // 图片实例化成功后存起来
                let bgImg = image;
                let setH = 0;
                let pages = [];
                for (let i = 0; i < num; i++) {
                  pages[i] = images.slice(i * 30, i * 30 + 30);
                }
                pages.forEach((page, k) => {
                  context.drawImage(bgImg, 0, setH, 3000, 2000);
                  // 开始绘制单页
                  // startX,startY,rowNum,borderImg,imgWidth1,imgHeight,stepY,stepX
                  // 203,538 +(k*2100),10,borderImg
                  drawImagesArea(context, page, {
                    startX: 203,
                    startY: 538 + k * 2100,
                    rowNum: 10, //一行几个
                    borderImg: borderImg, //皮肤边框
                  });
                  // 绘制单页结束
                  setH += 2100;
                });
                const base64Url = canvas.toDataURL("image/jpeg", 0.7);

                resolve(base64Url);
              };
            })
            .catch((err) => {

              reject(err);
            });
        };
      })
      .catch((err) => {

        reject(err);
      });
  });
}
// 生成游戏首图
function makeTitleImgStart(data, list, attrs,huoquData) {
  if (data.gameName == "和平精英") {
    return makeTitleImgHpjy(data, list, attrs);
  }
  if (data.gameName.indexOf("王者荣耀") > -1) {
    return makeTitleImgWzry(data, list, attrs);
  }
  // LOL 英雄联盟
  if (data.gameId == 5) {
    return makeTitleImgLOL(data, list, attrs);
  }
  // LOLM 英雄联盟手游
  if (data.gameId == 25) {
    return makeTitleImgLOLm(data, list, attrs);
  }
  // 原神
  if (data.gameName.indexOf("原神") > -1) {
    return makeTitleImgYs(data, list, attrs);
  }
  // makeTitleImgCF CF 穿越火线
  if (data.gameId == 6) {
    return makeTitleImgCF(data, list, attrs);
  }
  if (data.gameName == "无畏契约") {
    return makeTitleImgWWQY(data, list, attrs);
  }
  if (data.gameId == 12) {
    return makeTitleImgYjwj(data, list, attrs);
  }
  if(data.gameId==10){
    // 火影忍者
    return makeTitleImgHYRZ(data, list, attrs);
  }
  if(data.gameId==67){
    // 三角洲
    return makeTitleImgSJZXD(data, list, attrs);
  }
  if(data.gameId==8){
    // 枪战王者
    return makeTitleImgQZWZ(data, list, attrs);
  }
  if(data.gameId==7){
    // QQ飞车
    return makeTitleImgQQspeed(data, list, attrs,huoquData);
  }


  let keys = attrs.filter((i) => {
    return (
      i.fieldType == "input" || i.fieldType == "number" || i.fieldType == "date"
    );
  });
  // 通用首图
  return new Promise((resolve, reject) => {
    if (!list || list == []) {
      reject("多选不能为空");
    }
    // 取出已选择项
    let arr = [];
    list = list.split(",");
    if (list && list.length) {
      attrs.forEach(async (item1) => {
        if (item1.fieldType == "checkbox" && item1.isEnable === "0") {
          item1.childAttributes.forEach(async (item2) => {
            if (list.indexOf(String(item2.id)) > -1) {
              arr.push(item2);
            }
          });
        } else if (item1.fieldType == "boxradio" && item1.isEnable === "0") {
          item1.childAttributes.forEach(async (item2) => {
            item2.childGameAttributes.forEach(async (item3) => {
              if (list.indexOf(String(item3.id)) > -1) {
                arr.push(item3);
              }
            });
          });
        }
      });
    }

    getURLBase64("https://imgage.maimaihao.com/logo/yjwjjj.jpg")
      .then((res) => {
        const imageaaa = new Image();
        imageaaa.src = res;
        imageaaa.onload = () => {
          const canvas = document.createElement("canvas");
          const context = canvas.getContext("2d");
          canvas.width = imageaaa.width;
          canvas.height = imageaaa.height;
          context.drawImage(imageaaa, 0, 0, imageaaa.width, imageaaa.height);
          /**  */
          let titlName = `${data.gameName}`;
          context.fillStyle = "#ffff00";
          context.font = "normal bold 135px Adobe Ming Std";
          context.fillText(
            titlName,
            (3000 - (titlName ? titlName.length : 0) * 135) / 2,
            280
          );
          context.font = "100px Adobe Ming Std";
          if (keys[0] && keys[0].aavName != "qita") {
            context.fillText(
              `${keys[0] ? keys[0].fieldName : ""} ${data[keys[0].aavName] ? data[keys[0].aavName] : "--"
              }`,
              150,
              522
            );
          }
          if (keys[1] && keys[1].aavName != "qita") {
            context.fillText(
              `${keys[1] ? keys[1].fieldName : ""} ${data[keys[1].aavName] ? data[keys[1].aavName] : "--"
              } `,
              150,
              700
            );
          }
          if (keys[2] && keys[2].aavName != "qita") {
            context.fillText(
              `${keys[2] ? keys[2].fieldName : ""} ${data[keys[2].aavName] ? data[keys[2].aavName] : "--"
              }`,
              1100,
              522
            );
          }
          if (keys[3] && keys[3].aavName != "qita") {
            context.fillText(
              `${keys[3] ? keys[3].fieldName : ""} ${data[keys[3].aavName] ? data[keys[3].aavName] : "--"
              }`,
              1100,
              700
            );
          }

          if (keys[4] && keys[4].aavName != "qita") {
            context.fillText(
              `${keys[4] ? keys[4].fieldName : ""} ${data[keys[4].aavName] ? data[keys[4].aavName] : "--"
              }`,
              2100,
              522
            );
          }
          if (keys[5] && keys[5].aavName != "qita") {
            context.fillText(
              `${keys[5] ? keys[5].fieldName : ""} ${data[keys[5].aavName] ? data[keys[5].aavName] : "--"
              }`,
              2100,
              700
            );
          }
          // 底部
          if (keys[6]) {
            context.fillText(
              `${data[keys[6].aavName] ? data[keys[6].aavName] : "--"}`,
              (3000 -
                (data[keys[6].aavName] ? data[keys[6].aavName].length : 0) *
                90) /
              2,
              2520
            );
          }

          // 皮肤字体
          context.font = "normal bold 100px Adobe Ming Std";
          let skin = arr.splice(0, 32);
          ////console.log("%c Line:431 🥤 skin", "color:#3f7cff", skin);
          var x1 = 150;
          var h = 920;
          skin.map((item, index) => {
            if (index < 32) {
              if (index % 4 == 0) {
                x1 = 150;
                //控制行高
                h = h + 150;
              }
              let nametxt = "";
              if (data.gameName == "王者荣耀") {
                nametxt = item.fieldName.split("-")[1]
                  ? item.fieldName.split("-")[1]
                  : item.fieldName.split("-")[0];
              } else {
                nametxt = item.fieldName.slice(0, 7);
              }

              context.fillText(nametxt, x1, h);
              //控制列间隔
              x1 = x1 + 700;
            }
          });
          const base64Url = canvas.toDataURL("image/jpeg", 0.3);
          resolve(base64Url);
        };
      })
      .catch((err) => {
        reject(err);
      });
  });
}



let obj = {
  makeDetailImg,
  makeLoLzhanjiImg,
  makeTitleImgStart,
  makeDetailImgMultiple
};
export default obj;
